package sdc.com.petscalendar.fragments;

import sdc.com.petscalendar.MainActivity;
import sdc.com.petscalendar.R;
import sdc.com.petscalendar.objects.Pet;
import sdc.com.petscalendar.utils.Statics;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PetEditFragment extends BaseFragment implements
		View.OnClickListener {
	private View mTabID, mTabHabit, mTabEvr, mTabLastTreament;
	private View mTabCurrent = null;
	private boolean saved = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (Statics.sPet.getId() == 0) {
			String title = Statics.sPet.getType() == Pet.DOG ? "Nouveau chien"
					: "Nouveau chat";
			setTile(title);

		} else {
			setTile(Statics.sPet.getName());
		}
		((MainActivity) getActivity()).showBtnNavLeft(true);
		((MainActivity) getActivity())
				.setBtnNavLeftOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						saved = true;
					}
				});
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (mView == null) {
			mView = inflater.inflate(R.layout.fragment_edit_pet, container,
					false);
		}
		return mView;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		mTabID = getView().findViewById(R.id.tab_id);
		mTabHabit = getView().findViewById(R.id.tab_habit);
		mTabEvr = getView().findViewById(R.id.tab_evironment);
		mTabLastTreament = getView().findViewById(R.id.tab_last_treament);

		mTabID.setOnClickListener(this);
		mTabHabit.setOnClickListener(this);
		mTabEvr.setOnClickListener(this);
		mTabLastTreament.setOnClickListener(this);

		mTabID.performClick();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tab_id:
			if (mTabCurrent != null) {
				mTabCurrent.setSelected(false);
			}
			mTabID.setSelected(true);
			mTabCurrent = mTabID;
			changeTab(new PetEditIDFragment());
			break;
		case R.id.tab_habit:
			if (mTabCurrent != null) {
				mTabCurrent.setSelected(false);
			}
			mTabHabit.setSelected(true);
			mTabCurrent = mTabHabit;
			changeTab(new PetEditHabbitFragment());
			break;
		case R.id.tab_evironment:
			if (mTabCurrent != null) {
				mTabCurrent.setSelected(false);
			}
			mTabEvr.setSelected(true);
			mTabCurrent = mTabEvr;
			changeTab(new PetEditEvironmentFragment());
			break;
		case R.id.tab_last_treament:
			if (mTabCurrent != null) {
				mTabCurrent.setSelected(false);
			}
			mTabLastTreament.setSelected(true);
			mTabCurrent = mTabLastTreament;
			changeTab(new PetEditLastTreamentFragment());
			break;
		default:
			break;
		}
	}

	private void changeTab(BaseFragment fragment) {
		getChildFragmentManager().beginTransaction()
				.replace(R.id.frm_edit_pet, fragment).commit();
	}

	@Override
	public void onDestroy() {
		((MainActivity) getActivity()).showBtnNavLeft(false);
		if (!saved) {
			Statics.sPet = null;
		}
		super.onDestroy();
	}

}
